package main.scala.chapter02
import scala.collection.mutable
object Test37_wordCount {
  def main(args: Array[String]): Unit = {
    val stringList = List("Hello Scala Hbase kafka",
      "Hello Scala Hbase",
      "Hello Scala", "Hello")
    //使用for循环
    val map = mutable.Map[String,Int]()
    val list = stringList.map(e => e.split(" ").toList)
    println(list)
    for(i<-list;j<-i) map.put(j,map.getOrElse(j,0)+1)
    println(map)
    //使用算子计算
    println(stringList.flatMap(e => e.split(" ").toList).groupBy(e=>e)
    .map(e=>{e._1->e._2.size}))
    println(stringList.flatMap(e => e.split(" ").toList).groupBy(e=>e)
      .mapValues(e=>e.size).toList.sortBy(-_._2).take(3))

  }
}
